package com.qire.manhua.controller;

import android.content.Context;
import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.EventController;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.BR;
import com.qire.manhua.databinding.FragmentRecommendUpdateBinding;
import com.qire.manhua.fragment.RecommendUpdateFragment;
import com.qire.manhua.model.viewModel.impl.RecommendUpdateViewModel;
import com.qire.manhua.model.viewModel.impl.RecommendUpdateViewModel.UpdateComicItemViewModel;
import com.qire.other.recorder.ComicActionRecorder;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

public class RecommendUpdateController extends EventController<RecommendUpdateFragment> implements AutoVariable {

    private RecommendUpdateViewModel viewModel;

    public RecommendUpdateController(@NonNull RecommendUpdateFragment lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        FragmentRecommendUpdateBinding viewDataBinder = owner.viewDataBinder();
        if(viewDataBinder != null) {
            viewDataBinder.setRecommendUpdateController(this);
        }
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.recommendUpdateController, this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(RecommendUpdateViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    /**
     * 上拉加载，加载更多数据
     * */
    public void refreshLayoutOnLoadMore(RefreshLayout refreshLayout) {
        viewModel.moreUpdateComicData();
    }

    /**
     * 下拉刷新，重新获取数据
     * */
    public void refreshLayoutOnRefresh(RefreshLayout refreshLayout) {
        viewModel.loadUpdateComicData();
    }

    /**
     * 数据错误，重新加载数据按钮
     * */
    public void errorLayoutOnClick(View view) {
        viewModel.loadUpdateComicData();
    }

    /**
     * 图片布局单击事件：跳转阅读页阅读最新章节
     * */
    public void gotoComicReadOnClick(View view) {

        UpdateComicItemViewModel itemViewModel = (UpdateComicItemViewModel) view.getTag();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicQuickReadPage)
                .withData("comicBookEntity", itemViewModel.toComicBookEntity())
                .navigation();

        // 行为数据统计
        doActionRecorde(itemViewModel, view.getContext());
    }

    /**
     * 全集按钮单击事件：跳转详情页
     * */
    public void gotoComicDetailsInfoBtnOnClick(View view) {
        UpdateComicItemViewModel itemViewModel = (UpdateComicItemViewModel) view.getTag();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicDetailPage)
                .withData("comicBookId", itemViewModel.toComicBookEntity().id)
                .navigation();
        // 行为数据统计
        doActionRecorde(itemViewModel, view.getContext());
    }

    private void doActionRecorde(UpdateComicItemViewModel itemViewModel, Context context) {
        ComicActionRecorder.build(ComicActionRecorder.count_index)
                .setComicInfo(itemViewModel.comicId, itemViewModel.comicName)
                .putParam("频道名", "更新")
                .putParam("图片顺序", String.valueOf(itemViewModel.serialNumber))
                .doRecorde(context);
    }
}
